import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;

public class STD {

    public static void main(String[] args) {
        Map<String, List<String>> classes = new HashMap<>();
        Map<String, Map<String, Integer>> groups = new HashMap<>(); // 小组及其评分
        Map<String, List<String>> students = new HashMap<>();

        while (true) {
            int option = showMenu();

            switch (option) {
                case 1:
                    addClass(classes);
                    break;
                case 2:
                    manageClass(classes);
                    break;
                case 3:
                    addGroup(groups);
                    break;
                case 4:
                    manageGroup(groups);
                    break;
                case 5:
                    addStudent(students);
                    break;
                case 6:
                    manageStudent(students);
                    break;
                case 7:
                    randomRollCall(classes, groups, students);
                    break;
                case 8:
                    gradeGroup(groups);
                    break;
                case 9:
                    recordAttendance(students);
                    break;
                case 10:
                    recordAnswer(students);
                    break;
                case 11:
                    exportScores(classes, groups, students);
                    break;
                case 12:
                    JOptionPane.showMessageDialog(null, "您选择了退出");
                    System.exit(0);
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "无效的选项，请重新选择");
            }
        }
    }

    private static int showMenu() {
        Object[] options = {
                "1. 添加班级",
                "2. 管理班级",
                "3. 添加小组",
                "4. 管理小组",
                "5. 添加学生",
                "6. 管理学生",
                "7. 随机点名",
                "8. 给小组评分",
                "9. 记录学生考勤",
                "10. 记录学生答题情况",
                "11. 导出成绩",
                "12. 退出"
        };

        String selectedOption = (String) JOptionPane.showInputDialog(
                null,
                "请选择一个选项:",
                "菜单",
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[0]
        );

        if (selectedOption == null) {
            return -1; // 用户取消了选择
        }

        for (int i = 0; i < options.length; i++) {
            if (options[i].equals(selectedOption)) {
                return i + 1; // 返回选项编号
            }
        }

        return -1; // 无效选项
    }

    private static void addClass(Map<String, List<String>> classes) {
        String className = JOptionPane.showInputDialog("请输入班级名称:");
        if (className != null && !className.isEmpty()) {
            classes.put(className, new ArrayList<>());
            JOptionPane.showMessageDialog(null, "班级 " + className + " 已添加");
        } else {
            JOptionPane.showMessageDialog(null, "班级名称不能为空");
        }
    }

    private static void manageClass(Map<String, List<String>> classes) {
        if (classes.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有班级可以管理");
            return;
        }

        String[] classNames = classes.keySet().toArray(new String[0]);
        String selectedClass = (String) JOptionPane.showInputDialog(
                null,
                "请选择要管理的班级:",
                "管理班级",
                JOptionPane.QUESTION_MESSAGE,
                null,
                classNames,
                classNames[0]
        );

        if (selectedClass == null) {
            return;
        }

        int option = showClassManagementMenu();
        switch (option) {
            case 1:
                renameClass(classes, selectedClass);
                break;
            case 2:
                deleteClass(classes, selectedClass);
                break;
            case 3:
                JOptionPane.showMessageDialog(null, "查看班级信息功能尚未实现");
                break;
            case 4:
                addStudentToClass(classes, selectedClass);
                break;
            case 5:
                removeStudentFromClass(classes, selectedClass);
                break;
            default:
                JOptionPane.showMessageDialog(null, "无效的选项，请重新选择");
        }
    }

    private static int showClassManagementMenu() {
        Object[] options = {
                "1. 重命名班级",
                "2. 删除班级",
                "3. 查看班级信息",
                "4. 添加学生到班级",
                "5. 从班级中移除学生"
        };

        String selectedOption = (String) JOptionPane.showInputDialog(
                null,
                "请选择一个选项:",
                "管理班级",
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[0]
        );

        if (selectedOption == null) {
            return -1; // 用户取消了选择
        }

        for (int i = 0; i < options.length; i++) {
            if (options[i].equals(selectedOption)) {
                return i + 1; // 返回选项编号
            }
        }

        return -1; // 无效选项
    }

    private static void renameClass(Map<String, List<String>> classes, String oldName) {
        String newName = JOptionPane.showInputDialog("请输入新的班级名称:");
        if (newName != null && !newName.isEmpty()) {
            List<String> students = classes.get(oldName);
            classes.remove(oldName);
            classes.put(newName, students);
            JOptionPane.showMessageDialog(null, "班级 " + oldName + " 已重命名为 " + newName);
        } else {
            JOptionPane.showMessageDialog(null, "班级名称不能为空");
        }
    }

    private static void deleteClass(Map<String, List<String>> classes, String className) {
        classes.remove(className);
        JOptionPane.showMessageDialog(null, "班级 " + className + " 已删除");
    }

    private static void addStudentToClass(Map<String, List<String>> classes, String className) {
        String studentName = JOptionPane.showInputDialog("请输入学生姓名:");
        if (studentName != null && !studentName.isEmpty()) {
            classes.get(className).add(studentName);
            JOptionPane.showMessageDialog(null, "学生 " + studentName + " 已添加到班级 " + className);
        } else {
            JOptionPane.showMessageDialog(null, "学生姓名不能为空");
        }
    }

    private static void removeStudentFromClass(Map<String, List<String>> classes, String className) {
        List<String> students = classes.get(className);
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "班级 " + className + " 没有学生可以移除");
            return;
        }

        String[] studentNames = students.toArray(new String[0]);
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要移除的学生:",
                "移除学生",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentNames,
                studentNames[0]
        );

        if (selectedStudent == null) {
            return;
        }

        students.remove(selectedStudent);
        JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 已从班级 " + className + " 中移除");
    }

    private static void addGroup(Map<String, Map<String, Integer>> groups) {
        String groupName = JOptionPane.showInputDialog("请输入小组名称:");
        if (groupName != null && !groupName.isEmpty()) {
            groups.put(groupName, new HashMap<>());
            JOptionPane.showMessageDialog(null, "小组 " + groupName + " 已添加");
        } else {
            JOptionPane.showMessageDialog(null, "小组名称不能为空");
        }
    }

    private static void manageGroup(Map<String, Map<String, Integer>> groups) {
        if (groups.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有小组可以管理");
            return;
        }

        String[] groupNames = groups.keySet().toArray(new String[0]);
        String selectedGroup = (String) JOptionPane.showInputDialog(
                null,
                "请选择要管理的小组:",
                "管理小组",
                JOptionPane.QUESTION_MESSAGE,
                null,
                groupNames,
                groupNames[0]
        );

        if (selectedGroup == null) {
            return;
        }

        int option = showGroupManagementMenu();
        switch (option) {
            case 1:
                renameGroup(groups, selectedGroup);
                break;
            case 2:
                deleteGroup(groups, selectedGroup);
                break;
            case 3:
                JOptionPane.showMessageDialog(null, "查看小组信息功能尚未实现");
                break;
            case 4:
                addStudentToGroup(groups, selectedGroup);
                break;
            case 5:
                removeStudentFromGroup(groups, selectedGroup);
                break;
            default:
                JOptionPane.showMessageDialog(null, "无效的选项，请重新选择");
        }
    }

    private static int showGroupManagementMenu() {
        Object[] options = {
                "1. 重命名小组",
                "2. 删除小组",
                "3. 查看小组信息",
                "4. 添加学生到小组",
                "5. 从小组中移除学生"
        };

        String selectedOption = (String) JOptionPane.showInputDialog(
                null,
                "请选择一个选项:",
                "管理小组",
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[0]
        );

        if (selectedOption == null) {
            return -1; // 用户取消了选择
        }

        for (int i = 0; i < options.length; i++) {
            if (options[i].equals(selectedOption)) {
                return i + 1; // 返回选项编号
            }
        }

        return -1; // 无效选项
    }

    private static void renameGroup(Map<String, Map<String, Integer>> groups, String oldName) {
        String newName = JOptionPane.showInputDialog("请输入新的小组名称:");
        if (newName != null && !newName.isEmpty()) {
            Map<String, Integer> scores = groups.get(oldName);
            groups.remove(oldName);
            groups.put(newName, scores);
            JOptionPane.showMessageDialog(null, "小组 " + oldName + " 已重命名为 " + newName);
        } else {
            JOptionPane.showMessageDialog(null, "小组名称不能为空");
        }
    }

    private static void deleteGroup(Map<String, Map<String, Integer>> groups, String groupName) {
        groups.remove(groupName);
        JOptionPane.showMessageDialog(null, "小组 " + groupName + " 已删除");
    }

    private static void addStudentToGroup(Map<String, Map<String, Integer>> groups, String groupName) {
        String studentName = JOptionPane.showInputDialog("请输入学生姓名:");
        if (studentName != null && !studentName.isEmpty()) {
            groups.get(groupName).put(studentName, 0); // 默认分数为0
            JOptionPane.showMessageDialog(null, "学生 " + studentName + " 已添加到小组 " + groupName);
        } else {
            JOptionPane.showMessageDialog(null, "学生姓名不能为空");
        }
    }

    private static void removeStudentFromGroup(Map<String, Map<String, Integer>> groups, String groupName) {
        Map<String, Integer> students = groups.get(groupName);
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "小组 " + groupName + " 没有学生可以移除");
            return;
        }

        String[] studentNames = students.keySet().toArray(new String[0]);
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要移除的学生:",
                "移除学生",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentNames,
                studentNames[0]
        );

        if (selectedStudent == null) {
            return;
        }

        students.remove(selectedStudent);
        JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 已从小组 " + groupName + " 中移除");
    }

    private static void addStudent(Map<String, List<String>> students) {
        String studentName = JOptionPane.showInputDialog("请输入学生姓名:");
        if (studentName != null && !studentName.isEmpty()) {
            students.put(studentName, new ArrayList<>());
            JOptionPane.showMessageDialog(null, "学生 " + studentName + " 已添加");
        } else {
            JOptionPane.showMessageDialog(null, "学生姓名不能为空");
        }
    }

    private static void manageStudent(Map<String, List<String>> students) {
        if (students.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有学生可以管理");
            return;
        }

        String[] studentNames = students.keySet().toArray(new String[0]);
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要管理的学生:",
                "管理学生",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentNames,
                studentNames[0]
        );

        if (selectedStudent == null) {
            return;
        }

        int option = showStudentManagementMenu();
        switch (option) {
            case 1:
                renameStudent(students, selectedStudent);
                break;
            case 2:
                deleteStudent(students, selectedStudent);
                break;
            case 3:
                JOptionPane.showMessageDialog(null, "查看学生信息功能尚未实现");
                break;
            default:
                JOptionPane.showMessageDialog(null, "无效的选项，请重新选择");
        }
    }

    private static int showStudentManagementMenu() {
        Object[] options = {
                "1. 重命名学生",
                "2. 删除学生",
                "3. 查看学生信息"
        };

        String selectedOption = (String) JOptionPane.showInputDialog(
                null,
                "请选择一个选项:",
                "管理学生",
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[0]
        );

        if (selectedOption == null) {
            return -1; // 用户取消了选择
        }

        for (int i = 0; i < options.length; i++) {
            if (options[i].equals(selectedOption)) {
                return i + 1; // 返回选项编号
            }
        }

        return -1; // 无效选项
    }

    private static void renameStudent(Map<String, List<String>> students, String oldName) {
        String newName = JOptionPane.showInputDialog("请输入新的学生姓名:");
        if (newName != null && !newName.isEmpty()) {
            List<String> info = students.get(oldName);
            students.remove(oldName);
            students.put(newName, info);
            JOptionPane.showMessageDialog(null, "学生 " + oldName + " 已重命名为 " + newName);
        } else {
            JOptionPane.showMessageDialog(null, "学生姓名不能为空");
        }
    }

    private static void deleteStudent(Map<String, List<String>> students, String studentName) {
        students.remove(studentName);
        JOptionPane.showMessageDialog(null, "学生 " + studentName + " 已删除");
    }

    private static void randomRollCall(Map<String, List<String>> classes, Map<String, Map<String, Integer>> groups, Map<String, List<String>> students) {
        // 随机点名的逻辑
        JOptionPane.showMessageDialog(null, "随机点名功能尚未实现");
    }

    private static void gradeGroup(Map<String, Map<String, Integer>> groups) {
        if (groups.isEmpty()) {
            JOptionPane.showMessageDialog(null, "没有小组可以评分");
            return;
        }

        String[] groupNames = groups.keySet().toArray(new String[0]);
        String selectedGroup = (String) JOptionPane.showInputDialog(
                null,
                "请选择要评分的小组:",
                "给小组评分",
                JOptionPane.QUESTION_MESSAGE,
                null,
                groupNames,
                groupNames[0]
        );

        if (selectedGroup == null) {
            return;
        }

        Map<String, Integer> groupScores = groups.get(selectedGroup);
        if (groupScores.isEmpty()) {
            JOptionPane.showMessageDialog(null, "小组 " + selectedGroup + " 没有学生可以评分");
            return;
        }

        String[] studentNames = groupScores.keySet().toArray(new String[0]);
        String selectedStudent = (String) JOptionPane.showInputDialog(
                null,
                "请选择要评分的学生:",
                "给学生评分",
                JOptionPane.QUESTION_MESSAGE,
                null,
                studentNames,
                studentNames[0]
        );

        if (selectedStudent == null) {
            return;
        }

        String scoreStr = JOptionPane.showInputDialog("请输入学生的分数:");
        try {
            int score = Integer.parseInt(scoreStr);
            groupScores.put(selectedStudent, score);
            JOptionPane.showMessageDialog(null, "学生 " + selectedStudent + " 的分数已更新为 " + score);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "输入的分数无效，请输入一个整数");
        }
    }

    private static void recordAttendance(Map<String, List<String>> students) {
        // 记录学生考勤的逻辑
        JOptionPane.showMessageDialog(null, "记录学生考勤功能尚未实现");
    }

    private static void recordAnswer(Map<String, List<String>> students) {
        // 记录学生答题情况的逻辑
        JOptionPane.showMessageDialog(null, "记录学生答题情况功能尚未实现");
    }

    private static void exportScores(Map<String, List<String>> classes, Map<String, Map<String, Integer>> groups, Map<String, List<String>> students) {
        // 导出成绩的逻辑
        JOptionPane.showMessageDialog(null, "导出成绩功能尚未实现");
    }
}
